package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.query.DestinationThemeQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;

    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(destinationMapper.selectForList(qo));
    }

    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.selectDestByDeep(deep);
    }

    public List<Destination> getDestByRegionId(Long regionId) {
        List<Destination> list = new ArrayList<>();
        //通过区域id拿到ref，再通过get方法切割拿到ref的数组
        Long[] refIds = regionMapper.selectByPrimaryKey(regionId).getRefIds();
        for (Long refId : refIds) {
            //再根据每个ref找到目的地，放入list集合中
            list.add(destinationMapper.selectByPrimaryKey(refId));
        }
        return list;
    }

    public void delete(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    public void setInfo(Long id, String info) {
        destinationMapper.updateInfo(id,info);
    }

    public void changeHotValue(Long id, int hot) {
        destinationMapper.changeHotValue(id,hot);
    }

    public List<Destination> getToasts(Long parentId) {
        if (parentId == null){
            return null;
        }
        List<Destination> toasts = new ArrayList<>();
        createToasts(parentId,toasts);
        //将集合反转一下前后顺序
        Collections.reverse(toasts);
        return toasts;
    }

    public List<Destination> getHotDestByRegionId(Long regionId) {
        if(regionId == -1){
            return destinationMapper.selectByParentId(1L);
        }
        List<Destination> list = new ArrayList<>();
        //通过区域id拿到ref，再通过get方法切割拿到ref的数组
        Long[] refIds = regionMapper.selectByPrimaryKey(regionId).getRefIds();
        for (Long refId : refIds) {
            //再根据每个ref找到目的地，放入list集合中
            list.add(destinationMapper.selectHotDestByRegionId(refId));
        }
        return list;
    }

    public Destination getDestById(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    public Destination getCountry(Long id) {
        List<Destination> toasts = getToasts(id);
        if(toasts.size() >= 1){
            return toasts.get(0);
        }
        return null;
    }

    public Destination getProvince(Long id) {
        List<Destination> toasts = getToasts(id);
        if(toasts.size() >= 2 && toasts.get(0).getId() == 1){
            return toasts.get(1);
        }
        return null;
    }

    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    public String getSurveyById(Long destId) {
        return destinationMapper.selectSurveyById(destId);
    }

    @Override
    public PageInfo queryByCondition(DestinationThemeQuery qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = destinationMapper.selectByCondition(qo);

        return new PageInfo(list);
    }

    @Override
    public List<Destination> getDestByMonthId(Long id) {
        return destinationMapper.getDestByMonthId(id);
    }

    private void createToasts(Long parentId,List<Destination> toasts){
        //先查出当前的目的地，加入到面包屑中
        Destination dest = destinationMapper.selectByPrimaryKey(parentId);
        toasts.add(dest);
        //如果当前的目的地有父目的地，则也查出来加入到面包屑中，通过递归来完成
        if (dest.getParent() != null){
            createToasts(dest.getParent().getId(),toasts);
        }
    }
}
